package com.da.javatest.niuke.baodian;

import java.util.ArrayList;
import java.util.List;

import com.da.javatest.niuke.common.ListNode;
import com.da.javatest.niuke.common.TreeNode;

/**
 * @author chenlida
 * @date 2020/10/13 20:28
 * https://www.nowcoder.com/practice/adf889912fa441ea80a7c038baa1e2bb?tpId=8&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C71Converter {
    private List<Integer> list = new ArrayList<>();

    public ListNode treeToList(TreeNode root) {
        search(root);
        ListNode head = new ListNode(-1);
        ListNode node = head;
        for (Integer integer : list) {
            node.next = new ListNode(integer);
            node = node.next;
        }
        return head.next;
    }

    private void search(TreeNode root) {
        if (root != null) {
            search(root.left);
            list.add(root.val);
            search(root.right);
        }
    }

    private ListNode head = new ListNode(-1);
    private ListNode p = head;

    public ListNode treeToList2(TreeNode root) {
        if (root != null) {
            treeToList2(root.left);
            p.next = new ListNode(root.val);
            p = p.next;
            treeToList2(root.right);
        }
        return head.next;
    }
}
